// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const md5 = require('md5');
var request = require('request-promise');
exports.main = async (event, context) => {

	console.log(event);
	var xh = event.xh;
	var term = event.term;
	var week = event.week;
	var schoolYear = event.schoolYear;
	if (xh == undefined || week == undefined || xh == '' || xh == null) {
		return event
	}
	if (schoolYear == undefined) {
		schoolYear = '2020-2021';
	}
	if (term == undefined) {
		term = 1;
	}

	var temp =
		'{"code":"40001","message":"成功","otherMsg":null,"content":{"maxWeek":"22","startDay":"2020-06-01","endDay":"2020-06-07","list":[{"id":"AC5BA05C6E563629E053060011AC2402","courseName":"公共体育IV","week":"16","teacherName":"徐勇","classroomName":"田径场6","weekDay":"3","startTime":"1","endTime":"2","isCustom":"NO","className":null},{"id":"AC5BA05D55F93629E053060011AC2402","courseName":"计算机组成原理","week":"16","teacherName":"万杰","classroomName":"3128","weekDay":"5","startTime":"1","endTime":"2","isCustom":"NO","className":null},{"id":"AC5BA05D61703629E053060011AC2402","courseName":"Web程序设计基础","week":"16","teacherName":"周雄","classroomName":"2511","weekDay":"2","startTime":"1","endTime":"2","isCustom":"NO","className":null},{"id":"AC5BA05D4AA53629E053060011AC2402","courseName":"数据库原理及应用","week":"16","teacherName":"吴媛媛","classroomName":"2404","weekDay":"3","startTime":"3","endTime":"4","isCustom":"NO","className":null},{"id":"AC5BA05D5ED53629E053060011AC2402","courseName":"★无线传感器网络技术及应用","week":"16","teacherName":"王志豪","classroomName":"3501","weekDay":"3","startTime":"9","endTime":"10","isCustom":"NO","className":null},{"id":"AC5BA05D61973629E053060011AC2402","courseName":"Web程序设计基础","week":"16","teacherName":"周雄","classroomName":"物联网高技能培训中心（4408）","weekDay":"4","startTime":"9","endTime":"10","isCustom":"NO","className":null},{"id":"AC5BA05D5EAB3629E053060011AC2402","courseName":"★无线传感器网络技术及应用","week":"16","teacherName":"王志豪","classroomName":"2513","weekDay":"2","startTime":"3","endTime":"4","isCustom":"NO","className":null},{"id":"AC5BA05D65293629E053060011AC2402","courseName":"职业素质（四）","week":"16","teacherName":"曾梦韦","classroomName":null,"weekDay":"7","startTime":"7","endTime":"8","isCustom":"NO","className":null},{"id":"AC5BA05D4ACD3629E053060011AC2402","courseName":"数据库原理及应用","week":"16","teacherName":"吴媛媛","classroomName":"3229","weekDay":"5","startTime":"3","endTime":"4","isCustom":"NO","className":null},{"id":"AC5BA05D65283629E053060011AC2402","courseName":"职业素质（四）","week":"16","teacherName":"曾梦韦","classroomName":null,"weekDay":"6","startTime":"1","endTime":"2","isCustom":"NO","className":null},{"id":"AC5BA05D652E3629E053060011AC2402","courseName":"职业素质（四）","week":"16","teacherName":"曾梦韦","classroomName":null,"weekDay":"6","startTime":"3","endTime":"4","isCustom":"NO","className":null},{"id":"AC5BA05D65273629E053060011AC2402","courseName":"示例课程","week":"16","teacherName":"开学后正常显示","classroomName":"3415","weekDay":"1","startTime":"5","endTime":"6","isCustom":"NO","className":null},{"id":"AC5BA05D652A3629E053060011AC2402","courseName":"示例课程","week":"16","teacherName":"开学后正常显示","classroomName":"3415","weekDay":"1","startTime":"7","endTime":"8","isCustom":"NO","className":null},{"id":"AC5BA05D652D3629E053060011AC2402","courseName":"职业素质（四）","week":"16","teacherName":"曾梦韦","classroomName":null,"weekDay":"7","startTime":"1","endTime":"2","isCustom":"NO","className":null},{"id":"AC5BA05D652B3629E053060011AC2402","courseName":"示例课程","week":"16","teacherName":"开学后正常显示","classroomName":"3415","weekDay":"1","startTime":"3","endTime":"4","isCustom":"NO","className":null},{"id":"AC5BA05D652C3629E053060011AC2402","courseName":"示例课程","week":"16","teacherName":"开学后正常显示","classroomName":"3415","weekDay":"7","startTime":"5","endTime":"6","isCustom":"NO","className":null}]}}'

	// return temp

	var timestamp = (new Date()).valueOf();
	/*2.设置加密的数据*/
	var orginalData =
		'/normalservice/course/selectCourse?appKey=9b5e431b1a154e20a93357c9c0b732e4&clientCategory=PC&isWeek=YES&nonce=' +
		timestamp + '&operatorId=' +
		xh + '&schoolYear=' +
		schoolYear + '&term=' +
		term + '&timestamp=' +
		timestamp + '&universityId=102574&userType=STUDENT&week=' + week;
	var secr = '&appSecret=637208d1c1404cf4ad943f313ce21cd3';
	var md5str = md5(orginalData + secr).toUpperCase();
	const kk  = await request('http://ai.cqvie.edu.cn' + orginalData + '&sign=' + md5str).then(
		function (res) {
			return res;
		})
		.catch(function (err) {
			return temp;
		});
		return kk;
};